package com.sms.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sms.service.BisBusinessService;
import com.system.common.pojo.BisBusinessOrder;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisNurseType;
import com.system.common.pojo.BisShopUser;

@Controller
@RequestMapping("sms")
public class BisBusinessController {
	@Autowired
	private BisBusinessService bisBusinessService;
	
	private String price;

	@SuppressWarnings("static-access")
	@RequestMapping("businessshow")
	public String selectall(HttpServletRequest request, Map<String, Object> map, String orderId, String orderContent,
			String subscribeTime, String orderStatus, String username, String mobile, String qname, String qtype,
			String hour,HttpSession session) {
		BisShopUser user=(BisShopUser) session.getAttribute("shopUser");
		map.put("serverUserId", user.getShopUserId());
		map.put("handleUserId", user.getShopUserId());
		map.put("orderId", orderId);
		map.put("orderContent", orderContent);
		map.put("subscribeTime", subscribeTime);
		map.put("orderStatus", orderStatus);
		map.put("mobile", mobile);
		map.put("username", username);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		if (hour != null && !hour.equals("") && hour.equals("today")) {
			String today = format.format(date);
			hour = today;
		} else if (hour != null && !hour.equals("") && hour.equals("tomorrow")) {
			calendar.setTime(date);
			calendar.add(calendar.DATE, 1);
			date = calendar.getTime();
			String tomorrow = format.format(date);
			hour = tomorrow;
		} else if (hour != null && !hour.equals("") && hour.equals("dayafter")) {
			calendar.setTime(date);
			calendar.add(calendar.DATE, 2);
			date = calendar.getTime();
			String dayafter = format.format(date);
			hour = dayafter;
		}
		map.put("hour", hour);
		List<Map<String, Object>> olist = bisBusinessService.queryAll(map);
		map.put("olist", olist);
		Map<String, Object> map2 = initData(request);
		List<Map<String, Object>> pList = bisBusinessService.selectAll(map2);
		map.put("pList", pList);
		List<BisNurseType> tList = bisBusinessService.typeSelect();
		map.put("tList", tList);
		return "/sms/businessList";
	}

	public Map<String, Object> initData(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String qname = request.getParameter("qname");
		String qtype = request.getParameter("qtype");
		if (qname != null && !qname.equals("")) {
			map.put("qname", qname);
			request.setAttribute("qname", qname);
		}
		if (qtype != null && !qtype.equals("")) {
			map.put("qtype", qtype);
			request.setAttribute("qtype", qtype);
		}
		return map;
	}

	@RequestMapping("oUpdate")
	public String orderUpdate(String orderId) {
		if (orderId.indexOf(",") > 0) {
			orderId = orderId.substring(0, orderId.length() - 1);
		}
		BisBusinessOrder bisBusinessOrder = bisBusinessService.selectId(orderId);
		bisBusinessOrder.setOrderStatus("7");
		bisBusinessOrder.setCancelTime(new Date());
		bisBusinessService.update(bisBusinessOrder);
		return "redirect:/sms/businessshow";
	}

	@RequestMapping("selectId")
	@ResponseBody
	public void selectId(String orderIds, HttpServletResponse response) {
		if (orderIds.indexOf(",") > 0) {
			orderIds = orderIds.substring(0, orderIds.length() - 1);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("orderIds", orderIds);
		List<Map<String, Object>> single = bisBusinessService.queryAll(map);
		try {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(single);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("projectList")
	@ResponseBody
	public void projectSelect(String qname, String qtype, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("qname", qname);
		map.put("qtype", qtype);
		List<Map<String, Object>> pList = bisBusinessService.selectAll(map);
		try {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(pList);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}

    }

	@RequestMapping("changeOrders")		
	public String changeOrders(String korderId,HttpServletRequest request,HttpSession session) {
		BisShopUser user=(BisShopUser) session.getAttribute("shopUser");	
		String projectId=request.getParameter("project_id");
		BisBusinessOrder bisBusinessOrder = bisBusinessService.selectId(korderId);
		bisBusinessOrder.setProjectId(projectId);
		bisBusinessOrder.setOrderStatus("2");
		bisBusinessOrder.setServerUserId(user.getShopUserId());
		bisBusinessOrder.setHandleUserId(user.getShopUserId());
		bisBusinessService.update(bisBusinessOrder);
		return "redirect:/sms/businessshow";
	}

	@RequestMapping("queryId")
	public void queryId(String orderIdss, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("orderIds", orderIdss);
		List<Map<String, Object>> list = bisBusinessService.queryAll(map);
		try {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(list);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("saveMake")
	public String saveMake(HttpServletRequest request) {		
		String account = request.getParameter("account");
		String projectId = request.getParameter("projectId");
		String price = request.getParameter("price");
		String time = request.getParameter("time");
		String note = request.getParameter("note");
		String content = request.getParameter("content");
		bisBusinessService.addBisBusinessOrder(account, projectId, price, time, note, content);
		return "redirect:/sms/businessshow";
	}

	@RequestMapping("collect")
	public void collect(String payno, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("orderIds", payno);
		List<Map<String, Object>> mList = bisBusinessService.queryAll(map);
		try {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(mList);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("updateDetermine")
	public String updateDetermine(String tobepaid, HttpServletRequest request) {
		price=tobepaid;
		Map<String, Object> map = new HashMap<>();
		String orderId = request.getParameter("orderId");
		map.put("orderId", orderId);
		BisCustomUser bisCustomUser = bisBusinessService.queryId(map);
		int beautynum = bisCustomUser.getBeautyNum();
		int remaining = beautynum-Integer.parseInt(tobepaid);
		BisCustomUser bisCustomUser2 = new BisCustomUser();
		bisCustomUser2.setCustomUserId(bisCustomUser.getCustomUserId());
		bisCustomUser2.setUsername(bisCustomUser.getUsername());
		bisCustomUser2.setBeautyNum(remaining);
		if (bisBusinessService.updateBisCustomUser(bisCustomUser2)) {
			List<Map<String, Object>> mList = bisBusinessService.queryAll(map);
			for (Map<String, Object> map2 : mList) {
				for (Map.Entry<String, Object> m : map2.entrySet()) {
					if (m.getKey().equals("order_id")) {
						BisBusinessOrder bisBusinessOrder = bisBusinessService.selectId(m.getValue().toString());
						bisBusinessOrder.setOrderStatus("4");
						bisBusinessService.update(bisBusinessOrder);
					}
				}

			}
		}
		return "redirect:/sms/businessshow";
	}
}
